<?php

use yii\helpers\Html;
use yii\bootstrap\Modal;
use yii\helpers\Url;
use common\libs\Constants;
use mdm\admin\components\Helper;
use kartik\grid\GridView;
use backend\modules\spread\models\SpreadUrlSearch;

/* @var $this View */
/* @var $searchModel SpreadUrlSearch */
/* @var $dataProvider ActiveDataProvider */

$this->title = Yii::t('modules/spread/default', 'Spread Urls');
$this->params['breadcrumbs'][] = $this->title;

Modal::begin([
    'id'=>'operate-modal',
    'header'=>'<h4 class="modal-title"></h4>'
]);
Modal::end();
$createUrl = Url::toRoute('create');
$viewUrl = Url::toRoute('view');
$updateUrl = Url::toRoute('update');
$uploadUrl = Url::toRoute('upload');
$createTitle = Yii::t('modules/spread/default','Create Spread Url');
$updateTitle = Yii::t('modules/spread/default','Update Spread Url');
$viewTitle = Yii::t('modules/spread/default','View Spread Url');
$uploadTitle = Yii::t('modules/spread/default','Upload Spread Url');

$js = <<<JS
// 创建操作
$('#create').on('click', function () {
    $('.modal-dialog').removeClass('modal-lg');
    $('.modal-title').html('{$createTitle}');
    $.get('{$createUrl}',
        function (data) {
            $('.modal-body').html(data);
        }
    );
});
// 详情信息
$('.btn-view').on('click', function () {
    $('.modal-dialog').addClass('modal-lg');
    $('.modal-title').html('{$viewTitle}');
    $.get('{$viewUrl}', { id: $(this).closest('tr').data('key') },
        function (data) {
            $('.modal-body').html(data);
        }
    );
});
// 详情信息
$('.btn-upload').on('click', function () {
    $('.modal-dialog').addClass('modal-lg');
    $('.modal-title').html('{$uploadTitle}');
    $.get('{$uploadUrl}', { id: $(this).closest('tr').data('key') },
        function (data) {
            $('.modal-body').html(data);
        }
    );
});
// 更新操作
$('.btn-update').on('click', function () {
    $('.modal-dialog').removeClass('modal-lg');
    $('.modal-title').html('{$updateTitle}');
    $.get('{$updateUrl}', { id: $(this).closest('tr').data('key') },
        function (data) {
            $('.modal-body').html(data);
        }
    );
});
JS;
$this->registerJs($js);

?>
<div class="spread-url-index">
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>
    <?php if(Helper::checkRoute('create')) { ?>
    <p>
        <?= Html::a(Yii::t('modules/spread/default', 'Create Spread Url'), ['create'],
        [
            'class' => 'btn btn-success',
            'id' => 'create',
            'data-toggle' => 'modal',
            'data-target' => '#operate-modal'
        ]) ?>
    </p>
    <?php }?>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            [
                'attribute'=>'game_name',
                'value'=>'game.game_name',
                'label'=>'游戏名'
            ],
            [
                'attribute'=>'game_url',
                'value'=>'game_url',
                'filter'=>false
            ],
    [
        'class' => 'yii\grid\ActionColumn',
        'template' => Helper::filterActionColumn('{update}'),
        //                'header' => '操作',
        'headerOptions' => [
            'width' => '100px',
        ],
        'buttons' => [
            'update' => function($url, $model, $key) {
                return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, [
                           'title' => '编辑',
                           'class' => 'btn btn-default btn-update',
                           'data-toggle' => 'modal',
                           'data-target' => '#operate-modal',
                        ]);
            },
        ],
    ],
],
    ]); ?>
</div>